<?php

class Admini_ManageloginController extends Zend_Controller_Action {

    private $cdn = 'http://cdn.uuch.com';

    const COOKIE_USERNAME = '__xxadnnx';
    const COOKIE_IP = '__xxadppx';
    const COOKIE_TIME = '__xxadttx';

    public function indexAction() {
        $this->_helper->layout->setLayout('admin');
        header("Content-type: text/html; charset=utf-8");
        header("Expires:Mon, 26 Jul 1997 05:00:00 GMT");
        $this->appendCss('/admins/js/pagerView/pager-view.css');
        $this->appendCss('/admins/js/jquery/css/ui-lightness/jquery-ui-1.8.20.custom.css');
        $this->appendCss('/admins/js/jquery/jquery-ui-1.8.20.custom.min.js');
        $this->appendCss('/admins/js/jquery/jquery.ui.datepicker-zh-CN.js');
        $this->appendCss('/admins/js/pagerView/pager-view.js');
        $this->appendScript('/js/jquery.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js');
        $this->appendCss('/css/member/login.css');
        Application_Model_checkip::getbyip(0);
        $request = $this->getRequest();
        if ($request->isPost()) {
            $username = $request->getParam("username");
            $password = $request->getParam("password");
            if ($username != null && $password != null) {
//                $result = Application_Model_User::getbynikename($username);
                $result = Application_Model_adminuser::byloginname($username);
                if ($result != null && md5($password) == $result->password) {
                    //if($username=="uuadmin" && $password=="uuch.com.admin")
                    $result->lastlogin = date("Y-m-d H:s:i");
                    $result->lastip = $this->get_local_ip();
                    $result->loginnum = $result->loginnum + 1;
                    $result->save();
                    $this->setUser($username, 'uuch.com.admin');        
                    $returnUrl = $this->_getParam('returnUrl');
                    if (!$returnUrl) {
                        if ($result->loginnum == 1) {
                            $returnUrl = '/admini/managelogin/editpword/id/' . $result->id;
                        } else {
                            if($result->roleid==100){
                                $returnUrl = '/admini/agent/waybill';
                            }
                            else{
                                $returnUrl = '/admini/manageadmin/backlog';
                            }
                        }
                    }                    
                    $this->_redirect($returnUrl);
                }
                $this->view->flag = 'nopass';
            } else {
                $this->view->flag = 'nopass';
            }
        }
    }

    public function editpwordAction() {
        $this->appendScript('/js/jquery.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js');
        $this->appendCss('/css/member/login.css');
        $id = $this->_getParam("id");
        $request = $this->getRequest();
        if ($request->isPost()) {
            $password = $request->getParam("password");
            $repassword = $request->getParam("repassword");
            if ($repassword != null && $password != null && $password == $repassword) {
                $userid = $this->_getParam("userid");
                $result = Application_Model_adminuser::load($userid);
//                var_dump($result);exit;
                if ($result != null) {
                    $result->password = md5($password);
                    $result->lastlogin = date("Y-m-d H:s:i");
                    $result->lastip = $this->get_local_ip();
                    $result->loginnum = $result->loginnum + 1;
                    $result->save();
                    $this->setUser($result->loginname, 'uuch.com.admin');
                    $this->_redirect('/admini/manageuser/index/');
                }
                $this->view->flag = 'nopass';
            } else {
                $this->view->flag = 'nopass';
            }
        }
        $this->view->id = $id;
    }

    protected function setUser($userName, $remember) {
        $expire = 0;
        if ($remember)
            $expire = 0; //time()+5*3600;

        setcookie(self::COOKIE_TIME, Uuch_Util::encryptString(date('Y-m-d')), $expire, "/");
        setcookie(self::COOKIE_IP, Uuch_Util::encryptString(Uuch_Util::real_ip()), $expire, "/");
        setcookie(self::COOKIE_USERNAME, Uuch_Util::encryptString($userName), $expire, "/");
    }

    public function quitmanageAction() {
        setcookie('__xxadnnx', "", time() - 3600, "/");
        $this->_redirect('/admini/managelogin/index/');
    }

    /**
     * 往头部添加一个js文件
     * 这样封装的好处是将来我们可以直接在这个位置统一将所有的文件都换到别的域名上
     * 如jquery,可以用微软的CDN服务，或者我们自己布一个静态文件服务器
     * @param string $file
     * @return Uuch_BaseController
     */
    public function appendScript($file) {
        $temp = $this->cdn . strtolower($file);
        $this->view->headScript()->appendFile($temp);
        return $this;
    }

    /**
     * 往头文部里面添加一个CSS文件
     * @param string $css
     * @return Uuch_BaseController
     */
    public function appendCss($css) {
        $temp = $this->cdn . strtolower($css);
        $this->view->headLink()->appendStylesheet($temp);
        return $this;
    }

    public function get_local_ip() {
        if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
            $cip = $_SERVER["HTTP_CLIENT_IP"];
        } elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } elseif (!empty($_SERVER["REMOTE_ADDR"])) {
            $cip = $_SERVER["REMOTE_ADDR"];
        } else {
            $cip = "无法获取！";
        }
        return $cip;
    }
    
    public function tabletAction(){
        $this->_helper->layout->disableLayout();
    }
    
    public function tabletproccessAction(){
        $username = $this->_getParam("username");
        $password = $this->_getParam("password");
        if ($username != null && $password != null) {
            $result = Application_Model_adminuser::byloginname($username);
            if ($result != null && md5($password) == $result->password) {
                $result->lastlogin = date("Y-m-d H:s:i");
                $result->lastip = $this->get_local_ip();
                $result->loginnum = $result->loginnum + 1;
                $result->save();
                $this->setUser($username, 'uuch.com.admin'); 
                echo "1";exit();               
            }else{
                echo "0";exit();
            }
        }else{
            echo "0";exit();
        }
    }
    
    public function tabletlistAction(){
        $this->_helper->layout->disableLayout();
    }

}

